#ifndef __APP_MEASURE_H
#define __APP_MEASURE_H

#include "bsp_adc.h"
#include "bsp_gpio.h"


#define GD_WHEEL_COUNT						4
#define GD_LED_COUNT_PER_WHEEL				5

#define GD_DELAY_LED_WAIT					1

#define GD_ADC_TX_MIN						120
#define GD_ADC_TX_MAX						180
#define GD_ADC_RX_ERROR						40
#define GD_ADC_RX_THRESHOLD					128

#define GD_ERROR_ADC						0X01
#define GD_ERROR_RX_LED						0X10
#define GD_ERROR_TX_LED						0X20

#define GD_DATA_N						0X00
#define GD_DATA_O						0X10
#define GD_DATA_P						0X20
#define GD_DATA_0N						0X00
#define GD_DATA_0O						0X10
#define GD_DATA_0P						0X20
#define GD_DATA_9N						0X09
#define GD_DATA_9O						0X19
#define GD_DATA_9P						0X29

typedef struct
{
	uint8_t AdcDataTxLed[GD_WHEEL_COUNT>>1][GD_LED_COUNT_PER_WHEEL];
	uint8_t AdcDataRxLedOn[GD_WHEEL_COUNT][GD_LED_COUNT_PER_WHEEL];
	uint8_t AdcDataRxLedOff[GD_WHEEL_COUNT];
	uint8_t LogicDataX[GD_WHEEL_COUNT];
	uint8_t HexDataX[GD_WHEEL_COUNT];
	uint8_t BcdDataX[GD_WHEEL_COUNT];
	uint32_t DataX;
	uint8_t ErrorTxLed[GD_WHEEL_COUNT>>1];
	uint8_t ErrorRxLed;
	uint8_t Error;	
}GD_DATA_TYPE;

extern GD_DATA_TYPE GdData;

#define GD_enable_led_0()				{M0P_GPIO->PBOUT_f.PB03 = 0; M0P_GPIO->PBDIR_f.PB03 = 0;}
#define GD_enable_led_1()				{M0P_GPIO->PBOUT_f.PB04 = 0; M0P_GPIO->PBDIR_f.PB04 = 0;}
#define GD_enable_led_2()				{M0P_GPIO->PBOUT_f.PB05 = 0; M0P_GPIO->PBDIR_f.PB05 = 0;}
#define GD_enable_led_3()				{M0P_GPIO->PBOUT_f.PB06 = 0; M0P_GPIO->PBDIR_f.PB06 = 0;}
#define GD_enable_led_4()				{M0P_GPIO->PBOUT_f.PB07 = 0; M0P_GPIO->PBDIR_f.PB07 = 0;}

#define GD_enable_wheel_0_1()			M0P_GPIO->PAOUT_f.PA12 = 1; M0P_GPIO->PADIR_f.PA12 = 0;
#define GD_enable_wheel_2_3()			M0P_GPIO->PAOUT_f.PA15 = 1; M0P_GPIO->PADIR_f.PA15 = 0;

#define GD_RecvLED_Enable				M0P_GPIO->PDOUT_f.PD00 = 1  /*接收管使能*/
#define GD_RecvLED_Disable				M0P_GPIO->PDOUT_f.PD00 = 0

		
#define GD_SendLED_Disable				M0P_GPIO->PADIR |= 0X9000; /*发射管阳极设为输入*/ \
										M0P_GPIO->PBDIR |= 0X00F8;  /*发射管阴极设为输入*/

#define  DelayMs(Time)						Delay(Time*332)
#define  Delay( Time) { for (volatile uint16_t i=0; i<(Time); i++); }


void GD_Measure_Init(void);
int32_t GdGetVolumeData(void);

#endif
